package com.mokee.android.database;

import java.util.HashMap;
import java.util.List;

import android.util.Log;

import com.j256.ormlite.dao.RuntimeExceptionDao;

public class UserRecord {
	private static final String tag = "UserRecord";
	
	private RuntimeExceptionDao<UserTable, Integer> dao;

	public UserRecord(RuntimeExceptionDao<UserTable, Integer> dao) {
		this.dao = dao;
	}

	/** 查询全部的User */
	public List<UserTable> queryAllUser() {
		List<UserTable> userList = null;
		userList = dao.queryForAll();
		if (userList.size() > 0) {
			return userList;
		}
		return null;
	}
	
	/**  
	 * 根据Id查询
	 * @param id	UserTable的ID
	 * @return		查询成功：UserTable		查询失败：NULL
	 */
	public UserTable queryForId(int id){
		UserTable queryResult = dao.queryForId(id);
		if(queryResult != null){
			return queryResult;
		}
		return null;
	}
	
	/**
	 * 插入UserTable
	 * @param user
	 * @return
	 */
	public int insert(UserTable user){
		Log.i(tag, "UserRecord.insert:" + user.toString());
		int returnInt;
		UserTable table = queryForId(user.getId());
		if(table != null){
			returnInt = dao.update(user);
		} else {
			returnInt = dao.create(user);
		}
		return returnInt;
	}
	
	/**
	 * 根据处理进度查询
	 * @param process	处理进度
	 * @return	相同处理进度的所有UserTable的List
	 */
	public List<UserTable> queryForProcess(final int process){
		List<UserTable> list = dao.queryForFieldValues(new HashMap<String, Object>(){
			{
				put(UserTable.DT_Ship_Process, process);
			}
		});
		if(list.size() > 0){
			return list;
		}
		return null;
	}
	
	/**
	 * 更新进度
	 * @param table
	 */
	public void updata(UserTable table){
		dao.update(table);
	}
}
